using Aurora.AI.Common.Helper;
using Aurora.AI.SqlSugar.Framework;
using Aurora.AI.SqlSugar.Framework.Repositories;
using Aurora.AI.Wiki.Domain.Domain.ChatApplications.Aggregates;

namespace Aurora.AI.Wiki.Domain.Domain.ChatApplications.Repositories;

public sealed class ChatApplicationRepository
    : Repository<ChatApplication>, IChatApplicationRepository
{
    public Task<List<ChatApplication>> GetListAsync(int page, int pageSize)
    {
        return GetDb().Queryable<ChatApplication>().OrderByDescending(x => x.CreationTime)
            .ToOffsetPageAsync(page, pageSize);
    }

    public Task<int> GetCountAsync()
    {
        return GetDb().Queryable<ChatApplication>().CountAsync();
    }

    public async Task CreateApplication(ChatApplication application)
    {
        application.Id = Guid.NewGuid().ToString().Replace("-", "");
        await InsertAsync(application);
    }

    public async Task CreateChatRecord(ChatRecord record)
    {
        await GetDb().Insertable<ChatRecord>(record).ExecuteCommandAsync();
    }

    public async Task<ChatApplication> GetByIdAsync(string id)
    {
        return await base.GetByIdAsync(id);
    }

    public async Task<string> CreateChatDialogAsync(ChatApplicationDialog chatDialog)
    {
        chatDialog.Id = IdHelper.GenerateId();
        await GetDb().Insertable<ChatApplicationDialog>(chatDialog).ExecuteCommandAsync();
        return chatDialog.Id;
    }

    public async Task<bool> UpdateChatDialogAsync(ChatApplicationDialog chatDialog)
    {
        await GetDb().Updateable<ChatApplicationDialog>(chatDialog).ExecuteCommandAsync();
        return true;
    }

    public async Task<ChatApplicationDialog> GetChatDialogByIdAsync(string id)
    {
        return await GetDb().Queryable<ChatApplicationDialog>().InSingleAsync(id);
    }

    public async Task RemoveChatDialogAsync(string id)
    {
        await GetDb().Deleteable<ChatApplicationDialog>(id).ExecuteCommandAsync();
    }

    public async Task<List<ChatApplicationDialog>> GetChatDialogListAsync(string applicationId, string aiType)
    {
        return await GetDb().Queryable<ChatApplicationDialog>().Where(x => x.ApplicationId.Equals(applicationId)
                                                                           && x.AiType.Equals(aiType)
                                                                           && x.IsDeleted == false
        ).OrderByDescending(x => x.ModificationTime).ToListAsync();
    }

    public async Task<List<ChatRecord>> GetChatRecordListAsync(string dialogId)
    {
        return await GetDb().Queryable<ChatRecord>().Where(x => x.DialogId.Equals(dialogId))
            .OrderBy(x => x.CreationTime).ToListAsync();
    }

    // public async Task CreateChatDialogAsync(ChatDialog chatDialog)
    // {
    //     await GetDb().Insertable<ChatDialog>(chatDialog).ExecuteCommandAsync();
    // }
    //
    // public async Task RemoveChatDialogAsync(string id)
    // {
    //     var entity = await GetDb().Queryable<ChatDialog>().FirstAsync(x => x.Id.Equals(id));
    //
    //     if (entity != null)
    //     {
    //         await GetDb().Deleteable<ChatDialog>(entity).ExecuteCommandAsync();
    //     }
    // }
    //
    // public async Task<List<ChatDialog>> GetChatDialogListAsync()
    // {
    //     return await GetDb().Queryable<ChatDialog>().ToListAsync();
    // }
    //
    // public async Task CreateChatDialogHistoryAsync(ChatDialogHistory chatDialogHistory)
    // {
    //     await GetDb().Insertable<ChatDialogHistory>(chatDialogHistory).ExecuteCommandAsync();
    // }
    //
    // public async Task<List<ChatDialogHistory>> GetChatDialogHistoryListAsync(string chatDialogId, int page,
    //     int pageSize)
    // {
    //     return await GetDb().Queryable<ChatDialogHistory>().OrderByDescending(x => x.CreationTime)
    //         .ToOffsetPageAsync(page, pageSize);
    // }
    //
    // public async Task<long> GetChatDialogHistoryCountAsync(string chatDialogId)
    // {
    //     return await GetDb().Queryable<ChatDialogHistory>().CountAsync();
    // }
    //
    // public async Task RemoveChatDialogHistoryAsync(string chatDialogId)
    // {
    //     await GetDb().Deleteable<ChatDialogHistory>(x => x.ChatDialogId.Equals(chatDialogId)).ExecuteCommandAsync();
    // }
    //
    // public async Task RemoveChatDialogHistoryByIdAsync(string id)
    // {
    //     await GetDb().Deleteable<ChatDialogHistory>(x => x.Id.Equals(id)).ExecuteCommandAsync();
    // }

    public ChatApplicationRepository(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}